import React, { Component } from 'react'
// import { SettingAlert } from '../../components'
import { fetchData } from "../../../components/AjaxConsumer";
import { Modal, Button, Form } from 'antd';
import { ModalForm, BaseDataTreeComponent } from '../../../components'
//import { ModalForm, TreeComponent } from '../../components'

export default class AssignFactoryRes extends Component {
    constructor(props) {
        super(props);
        this.sendComponentCheckedKeys = [];
        this.state = {
            loading: false,
            visible: false,
            checkedKeys: '',
            factoryId: '',
        };
        this.showModal = this.showModal.bind(this)
        this.handleOk = this.handleOk.bind(this)
        this.handleCancel = this.handleCancel.bind(this)
    }

    //显示弹框
    showModal() {
        this.setState({
            visible: true,
        });
    }

    //提供调用初始化方法
    initModal(factoryId) {
        this.setState({
            factoryId: factoryId,
            //checkedKeys: ownResIds
        });
        this.getData(factoryId);
        //this.showModal();
    }
    //数据初始化
    getData = (factoryId) => {
        console.log("this.state.factoryId" + factoryId);
        let options = {
            url: '/base/factory/r/res/' + factoryId,//请求地址
            type: 'get'//请求方式

        };
        fetchData(this, options, (resData) => {
            let checkedKeys = [];
            let rtData = resData.data;
            for (let i = 0; i < rtData.length; i++) {
                checkedKeys.push(rtData[i].resourceId);
            }
            this.sendComponentCheckedKeys = checkedKeys;
            this.showModal();
        });

    }


    handCheckedKeys = (checkedKeys) => {
        this.sendComponentCheckedKeys = checkedKeys;
    }

    handleOk() {
        let factoryIdArray = [];
        factoryIdArray.push(this.state.factoryId);
        let factoryRes = [];
        console.log('--------------',this.sendComponentCheckedKeys)
        for (let i = 0; i < this.sendComponentCheckedKeys.length; i++) {
            let tmp = {};
            tmp.factoryId = this.state.factoryId;
            tmp.resourceId = this.sendComponentCheckedKeys[i];
            factoryRes.push(tmp);
        }

        let pstData = {};
        pstData.conditions = factoryIdArray;
        pstData.records = factoryRes;
        let options = {
            url: '/base/factory/u/res',//请求地址
            type: 'post',//请求方式
            condition: JSON.stringify(pstData),//请求条件
            contentType: 'application/json; charset=utf-8'
        };

        fetchData(this, options, this.handOkCallback);
    }

    handOkCallback = () => {
        this.setState({loading: false, visible: false});
    }

    handleCancel() {
        this.setState({visible: false});
    }

    render() {
        console.log(this.state.checkedKeys);
        let formAlerSize = 'small'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        return (
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title="分配资源"
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={formAlerSize=='large'?'850px':'400px'}
                footer={[
                             <Button key="back" size="large" onClick={this.handleCancel}>取消</Button>,
                             <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.handleOk}>提交</Button>
                           ]}
            >
                <div style={{minHeight:'450px'}}>
                    <BaseDataTreeComponent
                        checkable={true}
                        handCheckedKeys={this.handCheckedKeys}
                        checkedKeys={this.sendComponentCheckedKeys}
                        url="/base/resource/tree"
                        treeId='resourceId'
                        treeName='resourceName'
                        treeChildren='childResource'
                    />
                </div>
            </Modal>
        );
    }
}
